#include<iostream>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/objdetect.hpp>
#include<string>
#include<vector>
#include"func.h"

using namespace std;
using namespace cv;

Mat shibie(Mat mask,Mat img,vector<Point> &traj,bool& firstf)
{
    vector<vector<Point>>contours;
    // vector<Point> traj;
    findContours(mask,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);//寻找轮廓

    double maxArea=0;
    
    if(contours.empty())
    {

    }

    else
    {
        int maxContourIdx=-1;//目标索引
        for(int i=0;i<contours.size();i++)
        {
            double area=contourArea(contours[i]);
            if(area>maxArea&&area>5000)//找到大于4000的最大面积
            {
                maxArea=area;
                maxContourIdx=i;
            }
            

        }
        if(maxContourIdx==-1)//没有找到符合条件区域
        {

        }
        else
        {
            Rect boundingRect1;//目标
            boundingRect1=cv::boundingRect(contours[maxContourIdx]);
            rectangle(img,boundingRect1,Scalar(0,255,0),2);
            cout<<"最大面积：  "<<maxArea<<endl;
            Point2f centroid(boundingRect1.x+boundingRect1.width/2,boundingRect1.y+boundingRect1.height/2);
            cout<<"当前点位置: "<<centroid<<endl;
            Point2f mas;
            mas.x=centroid.x;
            mas.y=centroid.y;
            Point2f guji=yuce(mas,firstf);
            cout<<"预测点位置: "<<guji<<endl;
            circle(img,guji,10,Scalar(0,0,255),-1);//画圆函数(要绘制的图像，圆心坐标，半径，BGR颜色，轮廓宽度<负值为实心>)
            traj.push_back(centroid);
        }
        

    }

    firstf=0;//

    return img;
}